package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.weijianhuawen.wjblog.common.domain.dos.StatisticsArticlePvDo;

import java.time.LocalDate;
import java.util.List;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-11-01 16:17
 */

public interface StatisticsArticlePvMapper extends BaseMapper<StatisticsArticlePvDo> {
    // 对指定日期的pvj加1
    default int increasePvCount(LocalDate date) {
        return update(null, Wrappers.<StatisticsArticlePvDo>lambdaUpdate()
                .setSql("pv_count = pv_count + 1")
                .eq(StatisticsArticlePvDo::getPvDate, date));
    }

    // 查询最近一周的日pv量访问情况
    default List<StatisticsArticlePvDo> selectLatestWeekRecords() {
        return selectList(Wrappers.<StatisticsArticlePvDo>lambdaQuery()
                .le(StatisticsArticlePvDo::getPvDate, LocalDate.now().plusDays(1))  // 小于明天
                .orderByDesc(StatisticsArticlePvDo::getPvDate) // 降序排列
                .last("limit 7")); // 前7条数据
    }
}
